#include "mywebengine.h"
#include "iewidget.h"
#include "ui_iewidget.h"
IeWidget *myieWidget;
MyWebEngine::MyWebEngine(QWidget *parent):QWebEngineView(parent)
{

}
QWebEngineView * MyWebEngine::createWindow(QWebEnginePage::WebWindowType type)
{
    myieWidget = qobject_cast<IeWidget*>(window());
    if(!myieWidget)
        return NULL;
    else {
        switch (type) {
        case QWebEnginePage::WebBrowserTab:
            webengine = new MyWebEngine(this);
            myieWidget->ui->tabWidget->addTab(webengine,"");
            index = myieWidget->ui->tabWidget->count();
            myieWidget ->ui->tabWidget->setCurrentIndex(index-1);

            connect(webengine,&MyWebEngine::iconChanged,this,&MyWebEngine::changeIcon);

            connect(webengine,&MyWebEngine::urlChanged,this,&MyWebEngine::changeUrl);

            connect(webengine,&MyWebEngine::titleChanged,this,[=](QString str){
                myieWidget->ui->tabWidget->setTabText(index-1,str);
            });

            myieWidget->qlist.append(webengine);
            return webengine;
        default:
            break;
        }

        return this;
    }
}
void MyWebEngine::changeUrl()
{
    myieWidget->ui->url->setText(webengine->url().toString());
}

void MyWebEngine::changeIcon(QIcon icon)
{
    myieWidget->ui->tabWidget->setTabIcon(index-1,icon);
}
